Private Const OPEN_FLAGS = DEFAULT_FLAGS Or OFN_FILEMUSTEXIST
Private Const SAVE_FLAGS = DEFAULT_FLAGS Or OFN_OVERWRITEPROMPT
Private Type OPENFILENAME
lStructSize As Long ' <= Len(mOPENFILENAME)
hWndOwner As Long ' <= Owner
hInstance As Long ' not used here
lpstrFilter As String ' <= BuildFilterString
lpstrCustomFilter As String ' not used here
nMaxCustFilter As Long ' not used here
nFilterIndex As Long ' defaults to 1 (first filter)
lpstrFile As String ' <= DefaultFileName padded to MAX_PATH, PathFile =>
nMaxFile As Long ' <= length of lpstrFile
lpstrFileTitle As String ' <= MAX_PATH space reserved, FileTitle =>
nMaxFileTitle As Long ' <= length of lpstrFileTitle
lpstrDefaultDir As String ' <= DefaultDir (defaults to App.Path)
lpstrTitle As String ' <= Title (dialog caption)
Flags As Long ' <= OPEN_FLAGS or SAVE_FLAGS constructed constants
nFileOffset As Integer ' => position of FileNameOnly in lpstrFile)
nFileExtension As Integer ' => position of FileExtOnly in lpstrFile)
lpstrDefExt As String ' <= DefaultFileExt
lCustData As Long ' not used here
lpfnHook As Long ' not used here (see Const OFN_ENABLEHOOK)
lpTemplateName As String ' not used here
End Type
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long
'
' Module-level variables
'
Private mOPENFILENAME As OPENFILENAME
Private mFilters As VBA.Collection
'
' Local variables to hold property values
'
Private mvarOwner As Long
Private mvarDefaultDir As String
Private mvarDefaultFileName As String
Private mvarDefaultFileExt As String
Private mvarTitle As String
Private mvarPathFile As String
Private mvarPath As String
Private mvarFileName As String
Private mvarFileNameOnly As String
Private mvarFileExtOnly As String
Private mvarFileTitle As String
Private mvarPathFileShort As String ' relevant to OpenDialog only
'
' Enumerations
'
Public Enum DialogType
OpenDialog
SaveAsDialog
End Enum
'
' Write-only Properties
'
Public Property Let Owner(ByVal vData As Long)
Attribute Owner.VB_Description = "Window handle of calling form. (e.g., Form1.hWnd)"
mvarOwner = vData
End Property
Public Property Let DefaultDir(ByVal vData As String)
Attribute DefaultDir.VB_Description = "Initial file directory. Defaults to application path."
mvarDefaultDir = vData
End Property
Public Property Let DefaultFileName(ByVal vData As String)
Attribute DefaultFileName.VB_Description = "Filename used to initialize the File Name edit control."
mvarDefaultFileName = vData
End Property
Public Property Let DefaultFileExt(ByVal vData As String)
Attribute DefaultFileExt.VB_Description = "Extension appended to filename if user does not supply one. Only the first 3 characters are appended. A period (.) MUST NOT be included."
mvarDefaultFileExt = vData
End Property
Public Property Let Title(ByVal vData As String)
Attribute Title.VB_Description = "Window caption for dialog."
Public Function Show(Optional DlgType As DialogType = OpenDialog) As Boolean
Attribute Show.VB_Description = "Show modal dialog for ""Openf file"" or ""Save file"" (see dlgtype enumeration). Returns True if a file name available."
Attribute Show.VB_UserMemId = 0
On Error GoTo Show_Error
Dim Result As Boolean ' default function result = false